<?php


namespace App\Http\Controllers;


use App\Models\Department;
use App\Services\DepartmentService;
use App\Transformer\DepartmentTransformer;
use Dingo\Api\Http\Request;
use Illuminate\Support\Collection;

class DepartmentController extends Controller
{

    /**
     * 部门列表
     * @param Request $request
     * @return mixed
     * @author thanatos thanatos915@163.com
     */
    public function search(Request $request)
    {
        return $this->resArray(DepartmentService::search($request));
    }

    /**
     * 创建部门
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function create(Request $request)
    {
        can('department_manage');
        return $this->response->item(DepartmentService::createAndUpdate($request), new DepartmentTransformer());
    }

    /**
     * 部门详情
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function show($id)
    {
        return $this->response->item(DepartmentService::show($id), new DepartmentTransformer());
    }

    /**
     * 修改部门
     * @param $id
     * @param Request $request
     * @return \Dingo\Api\Http\Response
     * @author thanatos thanatos915@163.com
     */
    public function update($id, Request $request)
    {
        can('department_manage');
        return $this->response->item(DepartmentService::createAndUpdate($request, $id), new DepartmentTransformer());
    }

    /**
     * 删除部门
     * @param $id
     * @return \Dingo\Api\Http\Response
     * @throws \Exception
     * @author thanatos thanatos915@163.com
     */
    public function delete($id)
    {
        can('department_manage');
        DepartmentService::delete($id);
        return $this->response->noContent();
    }

}
